
from django.shortcuts import (get_object_or_404, redirect, render,
                              render_to_response)
from django.contrib.auth.models import User
from django.views.generic.list import ListView
from main import models,logic
import logging

logger = logging.getLogger('main')
# Create your views here.
def index(request):
    user = request.user
    if user and user.is_active:
        return render(request, 'main/index.html')
    else:
        return render(request, 'login/login.html')

def leftmenu(request):
    user = request.user
    
    return render(request, 'main/left.html')
   
class MainMuenListView(ListView):
    model = models.role
    template_name = 'main/index.html'

    def get_context_data(self, **kwargs):
        context = super(MainMuenListView, self).get_context_data(**kwargs)
        user_id=self.request.user.pk
        userinfo = models.user.objects.get(user_id=user_id)
        role = userinfo.role
        menu_id = str(role.rights).split(',')
        menulist = models.menu.objects.filter(id__in=menu_id).order_by("parent_id","order")
        
        menu_dict = logic.menu_struct(menulist)
        context['menu_dict'] = menu_dict
        logger.info("muen list struct complete")
        return context

def tab(request):
    return render(request, 'main/tab.html')


def main_default(request):
    return render(request, 'main/main_default.html')


def main_defaultlist(request):
    return render(request, 'main/main_defaultlist.html')

def main_defaultedit(request):
    return render(request, 'main/main_defaultedit.html')

 
class MainDefaultListView(ListView):
    model = models.menu
    template_name = 'main/main_defaultlist1.html'

    def get_context_data(self, **kwargs):
        context = super(MainDefaultListView, self).get_context_data(**kwargs)
        user_id=self.request.user.pk
        print self.request.GET.get("KEYWORD")
        userinfo = models.user.objects.get(user_id=user_id)
       
        role_id = userinfo.role_id
        context['sub_item'] = models.subitem.objects.filter(role_id=role_id)
        context['menu_dict'] = models.menu.objects.all()
         
        logger.info("muen list struct objects all")
        return context